#include <iostream>
using namespace std;
#include <string.h>
#include <assert.h>

namespace lw
{
    class string
    {
    public:
        typedef char* iterator;
        typedef const char* const_iterator;
        iterator begin()
        {
            return _str;
        }
        iterator end()
        {
            return _str + _size;
        }
        const_iterator begin() const
        {
            return _str;
        }
        const_iterator end() const
        {
            return _str + _size;
        }
        string(const char* str = "")
        {
            _size = strlen(str);
            _capacity = _size;
            _str = new char[_size + 1];

            strcpy(_str, str);
        }
        ~string()
        {
            delete[] _str;
            _str = nullptr;
            _size = _capacity = 0;
        }
        template<class ITERATOR>
        string(ITERATOR begin, ITERATOR end)
        {
            _size = _capacity = 0;
            _str = new char[1];
            _str[0] = '\0';

            while(begin != end)
            {
                push_back(*begin);
                ++begin;
            }
        }
        string(const string& str) 
            :_str(nullptr)
             ,_size(0)
             ,_capacity(0)
        {
            string tmp(str.begin(), str.end());
            swap(tmp);
        }
        string& operator=(string s) 
        {
            swap(s);
            return *this;
        }
        void swap(string& s)
        {
            ::swap(_str, s._str);
            ::swap(_size, s._size);
            ::swap(_capacity, s._capacity);
        }
        const char* c_str() const
        {
            return _str;
        }
        size_t size() const
        {
            return _size;
        }
        size_t capacity() const 
        {
            return _capacity;
        }
        bool empty() const 
        {
            return _size == 0;
        }
        char& operator[](size_t pos)
        {
            assert(pos < _size);

            return _str[pos];
        }
        const char& operator[](size_t pos) const 
        {
            assert(pos < _size);

            return _str[pos];
        }
        void reserve(size_t n)
        {
            if(n > _capacity)
            {
                char* tmp = new char[n + 1];
                strcpy(tmp, _str);
                delete[] _str;
                _str = tmp;
                _capacity = n;

                _str[n] = '\0';
            }
        }
        void resize(size_t n, char ch = '\0')
        {
            if(n > _capacity)
            {
                reserve(n);
            }
            else if(n > _size)
            {
                for (size_t i = _size; i < n; i++)
                {
                    _str[i] = ch;
                }
            }
             _str[n] = '\0';
             _size = n;
        }
        void push_back(char ch)
        {
            if(_size == _capacity)
            {
                reserve(_size == 0 ? 10 : 2 * _size);
            }
            _str[_size] = ch;
            _size++;
            _str[_size] = '\0';
        }
        void append(const char* str)
        {
            if(_size + strlen(str) >= _capacity)
            {
                reserve(_size + strlen(str));
            }
            strcpy(_str + _size, str);
            _size += strlen(str);
        }
        string& operator+=(char ch)
        {
            push_back(ch);
            return *this;
        }
        string& operator+=(const char* str)
        {
            append(str);
            return *this;
        }
        //////////////////////////////// compare
        bool operator>(const string& s) const 
        {
            return strcmp(_str, s._str) > 0;
        }
        bool operator==(const string& s) const 
        {
            return strcmp(_str, s._str) == 0;
        }
        bool operator>=(const string& s) const 
        {
            return *this > s || *this == s;
        }
        bool operator<(const string& s) const 
        {
            return !(*this >= s);
        }
        bool operator<=(const string& s) const 
        {
            return !(*this > s);
        }
        bool operator!=(const string& s) const 
        {
            return !(*this == s);
        }
        const static size_t npos = -1;
    private:
        char* _str;
        size_t _size;
        size_t _capacity;
    };
    void Test_string1()
    {
        string s1("hello world");
        cout << s1.c_str() << endl;

        string s2;
        cout << s2.c_str() << endl;
    }
    void Test_string2()
    {
        string s1("Hello World!");
        string::iterator it = s1.begin();
        while(it != s1.end())
        {
            cout << *it << " ";
            ++it;
        }
        cout << endl;

        for (auto ch : s1)
        {
            cout << ch << " ";
        }
        cout << endl;

        string s2;
        cout << (s1 > s2) << endl;
        cout << (s1 >= s2) << endl;
        cout << (s1 < s2) << endl;
        cout << (s1 <= s2) << endl;
        cout << (s1 == s2) << endl;
        cout << (s1 != s2) << endl;
    }
    void Test_string3()
    {
        string s1("Hello world!");
        cout << s1.size() << endl;
        cout << s1.capacity() << endl;
        cout << (void*)s1.c_str() << endl;
        cout << s1.c_str() << endl;

        s1.reserve(20);
        cout << s1.size() << endl;
        cout << s1.capacity() << endl;
        cout << (void*)s1.c_str() << endl;
        cout << s1.c_str() << endl;
    }
    void Test_string4()
    {
        string s1("hello world");
        s1.push_back('w');
        cout << s1.c_str() << endl;
        s1.resize(20, 'x');
        cout << s1.c_str() << endl;
        s1.resize(6);
        cout << s1.c_str() << endl;
        s1.append("world !");
        cout << s1.c_str() << endl;
        s1.push_back('c');
        cout << s1.c_str() << endl;
    }

}
